{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27beb3c1-4d05-4645-85e5-60c28bdaaae2",
   "metadata": {},
   "source": [
    "# NetworkQueryEngine Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff743622-6887-4121-8049-44110b366f06",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from llama_index.networks import (\n",
    "    NetworkQueryEngine,\n",
    "    ContributorClient,\n",
    ")\n",
    "from llama_index.llms.openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee0673fd-2bcc-4a45-8caa-a2714377ac5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1c19958-cb5d-49b4-af8a-76265f108286",
   "metadata": {},
   "outputs": [],
   "source": [
    "# setup ContributorClients to connect to ContributorServices\n",
    "contributors = [\n",
    "    ContributorClient.from_config_file(\n",
    "        env_file=f\"./client-env-files/.env.contributor_{ix}.client\"\n",
    "    )\n",
    "    for ix in range(1, 4)\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce075783-b9bc-4dbb-b982-e6c2c47a8a40",
   "metadata": {},
   "outputs": [],
   "source": [
    "# build NetworkRAG\n",
    "api_key = os.environ.get(\"OPENAI_API_KEY\")\n",
    "llm = OpenAI(api_key=api_key)\n",
    "network_query_engine = NetworkQueryEngine.from_args(contributors=contributors, llm=llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05350567-a369-4274-9574-cecb194f1d7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# knowledge from contributor 1\n",
    "response = await network_query_engine.aquery(\"Who is Paul?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ccb68f2-2c65-4074-bb7f-5c411ab8ef5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Paul is a person who has been involved in various ventures such as painting, starting companies like Viaweb and Y Combinator, and creating a new dialect of Lisp called Arc. He also began publishing essays online, realizing the potential of the web as a platform for reaching a wider audience.'"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f28db53-a96d-431a-aee9-bf16b236f850",
   "metadata": {},
   "outputs": [],
   "source": [
    "# knowledge from contributor 2\n",
    "response = await network_query_engine.aquery(\"What is DoRA?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03144a51-21ec-4667-858e-81baaea97507",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'DoRA is a method that enhances the learning capacity of LoRA by introducing incremental directional updates and adapting to different LoRA variants. It can be combined with other LoRA variants like VeRA, which reduces trainable parameters significantly while maintaining accuracy. DoRA consistently outperforms LoRA across various rank settings for commonsense reasoning tasks, demonstrating its ability to achieve better accuracy with fewer trainable parameters.'"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd5849c6-6ebf-4f69-bbd2-d874e2ee80d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# knowledge from contributor 3\n",
    "response = await network_query_engine.aquery(\"Summarize the history of New York City\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de285ade-451f-407d-a308-685da6b5fcb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"New York City's history began with European exploration in the early 16th century, leading to Dutch settlement and the founding of New Amsterdam in the early 17th century. It played a crucial role in the American Revolution and briefly served as the national capital. The city's strategic location as a major seaport and transportation hub, along with waves of European immigrants in the mid-19th century, fueled its industrial growth. The consolidation of the five boroughs in 1898 marked a significant milestone. Over the years, New York City has evolved into a global center for finance, culture, and commerce, known for its diverse population, iconic landmarks, and significant role in historical events.\""
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.response"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "networks-demo",
   "language": "python",
   "name": "networks-demo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
